#include "Math3D.h"

namespace egg {

    float Math3D::PI=3.1415926;

	bool Math3D::isNegative(const vec3& value) {
		return (value.x < 0 &&
			value.y < 0 &&
			value.z < 0);
	}

    float Math3D::deg2rad(float degree){
        return degree/180.0f*PI;
    }

    float Math3D::rad2deg(float radian){
        return radian/PI*180.0f;
    }

    void Math3D::computeTBN(
        uint32 p0,uint32 p1,uint32 p2,
        const vec3* pBuf,const vec2* tBuf,const vec3* nBuf,vec3* tanBuf)
    {
        {
            mat2 m1(tBuf[p1]-tBuf[p0],tBuf[p2]-tBuf[p0]);
            mat2x3 m2(pBuf[p1]-pBuf[p0],pBuf[p2]-pBuf[p0]);
            m1=glm::inverse(m1);
            mat2x3 TB=m2*m1;
            tanBuf[p0]=glm::normalize(glm::column(TB,0));
        }
        {
            mat2 m1(tBuf[p0]-tBuf[p1],tBuf[p2]-tBuf[p1]);
            mat2x3 m2(pBuf[p0]-pBuf[p1],pBuf[p2]-pBuf[p1]);
            m1=glm::inverse(m1);
            mat2x3 TB=m2*m1;
            tanBuf[p1]=glm::normalize(glm::column(TB,0));
        }
        {
            mat2 m1(tBuf[p0]-tBuf[p2],tBuf[p1]-tBuf[p2]);
            mat2x3 m2(pBuf[p0]-pBuf[p2],pBuf[p1]-pBuf[p2]);
            m1=glm::inverse(m1);
            mat2x3 TB=m2*m1;
            tanBuf[p2]=glm::normalize(glm::column(TB,0));
        }
    }

}

